package com.edge.pbx.view.home;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.edge.pbxdsp.R;
import com.qztech.sdk.ui.activities.BaseFragmentActivity;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class PbxMainMenuRootLayout extends FrameLayout implements View.OnClickListener {
    public static final int FIX_HEIGHT = 1440;
    private View mLayoutLeft, mLayoutRight;
    //934x1440
    public PbxMainMenuRootLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
    }

    @Override
    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        View view = null;

        int w = height*934/FIX_HEIGHT;
        view = getChildAt(0);
        if (view != null){
            view.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        }
        view = getChildAt(1);
        if (view != null){
            view.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        YzDebug.d("Yang","PbxMainMenuRootLayout onFinishInflate");
        findViewById(R.id.pbx_main_menu_left_close).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_right_close).setOnClickListener(this);

        findViewById(R.id.pbx_main_menu_home).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_save).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_upload).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_setting).setOnClickListener(this);

        findViewById(R.id.pbx_main_menu_summing).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_crossover).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_delay).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_gain).setOnClickListener(this);
        findViewById(R.id.pbx_main_menu_eq).setOnClickListener(this);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = r -l;
        int height = b -t;

        View view = getChildAt(0);
//        YzDebug.d("PBX","onLayout:"+ width +" " + height +"  " +view.getMeasuredWidth() +" " + view.getMeasuredHeight());
        if (view != null){
            view.layout(0,0,  view.getMeasuredWidth(), height);
        }

        view = getChildAt(1);
        if (view != null){

            view.layout(width - view.getMeasuredWidth(),0, width, view.getMeasuredHeight());
        }
    }


    private void setLeftLayoutVisibility(int visibility){
        if (mLayoutLeft == null){
            mLayoutLeft = findViewById(R.id.pbx_main_menu_left_layout);
        }
        if (mLayoutLeft != null){
            if (mLayoutLeft.getVisibility() != visibility){
                mLayoutLeft.setVisibility(visibility);
            }
        }
    }
    private void setRightLayoutVisibility(int visibility){
        if (mLayoutRight == null){
            mLayoutRight = findViewById(R.id.pbx_main_menu_right_layout);
        }
        if (mLayoutRight != null){
            if (mLayoutRight.getVisibility() != visibility){
                mLayoutRight.setVisibility(visibility);
            }
        }
    }

    public void showHideLeftMenu(boolean isShow){
        if (getVisibility() != VISIBLE)
            setVisibility(VISIBLE);
        setLeftLayoutVisibility(VISIBLE);
        setRightLayoutVisibility(GONE);
        if (mLayoutLeft != null){
            Animation animation = AnimationUtils.loadAnimation(getContext(),
                    isShow?R.anim.main_left_menu_show:R.anim.main_left_menu_hide);
            if (!isShow){
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        setVisibility(GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
            mLayoutLeft.startAnimation(animation);
        }
    }

    public void showHideRightMenu(boolean isShow){
        if (getVisibility() != VISIBLE)
            setVisibility(VISIBLE);
        setLeftLayoutVisibility(GONE);
        setRightLayoutVisibility(VISIBLE);
        if (mLayoutRight != null){
            Animation animation = AnimationUtils.loadAnimation(getContext(),
                    isShow? R.anim.main_right_menu_show:R.anim.main_right_menu_hide);
            if (!isShow){
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        setVisibility(GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
            mLayoutRight.startAnimation(animation);
        }
    }

    @Override
    public void onClick(View v) {
        BaseFragmentActivity activity = null;
        if (getContext() instanceof BaseFragmentActivity){
            activity = (BaseFragmentActivity) getContext();
        }
        switch (v.getId()){
            case R.id.pbx_main_menu_root_layout:
                if(mLayoutLeft.getVisibility() == View.VISIBLE)
                    showHideLeftMenu(false);
                else if(mLayoutRight.getVisibility() == View.VISIBLE)
                    showHideRightMenu(false);
                else
                    setVisibility(View.GONE);
                break;
            case R.id.pbx_main_menu_left_close:
                showHideLeftMenu(false);
                break;
            case R.id.pbx_main_menu_right_close:
                showHideRightMenu(false);
                break;
            case R.id.pbx_main_menu_home:
                if (activity != null){
                    activity.finish();
                }
                break;
            case R.id.pbx_main_menu_save:
                if (activity != null){
                    activity.showSaveDialog(false);
                }
                break;
            case R.id.pbx_main_menu_upload:
                if (activity != null){
                    activity.readData();
                }
                break;
            case R.id.pbx_main_menu_setting:
                if (activity != null){
                    activity.showDeviceDialog(false);
                }
                break;
            case R.id.pbx_main_menu_summing:
            case R.id.pbx_main_menu_crossover:
            case R.id.pbx_main_menu_delay:
            case R.id.pbx_main_menu_gain:
            case R.id.pbx_main_menu_eq:
//                try {
//                    Thread.sleep(Long.MAX_VALUE);
//                }catch (Exception e){}
                showHideRightMenu(false);
                if (mMenuClickListener != null){
                    mMenuClickListener.onClick(v);
                }

                break;
        }
    }

    private View.OnClickListener mMenuClickListener;

    public void setMenuClickListener(OnClickListener menuClickListener) {
        mMenuClickListener = menuClickListener;
    }
}
